package com.neuedu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.neuedu.entity.Employee;

import java.util.Date;

/**
 * @author LiYang
 * @desc
 * @date 2024/11/13 15:07
 */
public class JwtUtil {
    private static final String KEY ="LIYANG";
    public static String createToken(Employee employee){
        return JWT.create().withClaim("id",employee.getId())
                .withClaim("phone",employee.getPhone())
                .withClaim("name",employee.getRealname())
                .withClaim("time",new Date())
                .sign(Algorithm.HMAC256(KEY));
    }
    public static Employee decoder(String token){
        final DecodedJWT verify = JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token);
        Employee emp=new Employee();
        emp.setId(verify.getClaim("id").asLong());
        emp.setRealname(verify.getClaim("name").asString());
        emp.setPhone(verify.getClaim("phone").asString());
        return emp;
    }
}

